package com.xyz.business.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.xyz.base.dao.BaseDao;
import com.xyz.business.bean.User;
import com.xyz.business.bean.query.UserForm;
import com.xyz.business.dao.UserDao;
import com.xyz.business.response.UserQueryResponse;
import com.xyz.business.service.impl.UserServiceImpl;
import com.xyz.constant.XYZConstant;

public class UserDaoImpl extends BaseDao implements UserDao {
	/**
	 * 日子还是
	 */
	private static Logger log = Logger.getLogger(UserServiceImpl.class);

	/**
	 * 插入用户
	 * 
	 * @see com.xyz.business.dao.UserDao#insert(com.xyz.business.bean.User)
	 */
	public void insert(User user) {
		sqlMapClientTemplate.insert("insertUser", user);
	}

	@SuppressWarnings("unchecked")
	public UserQueryResponse queryUser(UserForm form) {
		List<User> users = new ArrayList<User>();
		// 总页数
		int rowsCount = XYZConstant.NUM_0;
		UserQueryResponse response = new UserQueryResponse();
		if (form != null) {
			String sql = " select uid,name,email from t_user where 1=1   and ishide=0 ";
			String whereSql = "";
			User user = form.getUser();
			int pageNow = form.getPageNow();
			int pageSize = form.getPageSize();
			if (user != null) {
				String name = user.getName();
				String email = user.getEmail();
				if (!StringUtils.isBlank(name)) {
					whereSql += " and name like '%" + name + "%'";
				}
				if (!StringUtils.isBlank(email)) {
					whereSql += " and emailn like '%" + email + "%'";
				}
			}
			whereSql += " limit " + (pageNow - 1) * pageSize + "," + pageSize;
			String countSql = " select count(uid)  from t_user ";
			rowsCount = queryUserCount(countSql);
			users = sqlMapClientTemplate.queryForList("queryUser", sql
					+ whereSql);

		}
		response.setReturnCode("0000");
		response.setUserList(users);
		response.setRowCount(rowsCount);
		return response;

	}

	private int queryUserCount(String countSql) {
		Integer rowCount = (Integer) sqlMapClientTemplate.queryForObject(
				"queryUserCount", countSql);
		if (rowCount == null) {
			return XYZConstant.NUM_0;
		} else {
			return rowCount;
		}
	}

	@Override
	public int queryUserByName(String loginname) throws SQLException {
		// TODO Auto-generated method stub
		return (Integer) sqlMapClient.queryForObject("queryUserByName",
				loginname);
	}

	@Override
	public int login(User user) throws SQLException {

		return (Integer) sqlMapClient.queryForObject("login", user);
	}

	@Override
	public void insertUser(User user) throws SQLException {
		Date now = new Date();
		user.setInsertdate(now);
		user.setUpdatetime(now);
		sqlMapClient.insert("insertUser", user);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> query() throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapClient.queryForList("qrAllUser");
	}

	@Override
	public User queryUser(String loginname) throws SQLException {
		// TODO Auto-generated method stub
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("select * from t_user where   ishide=0");
		if (!StringUtils.isBlank(loginname)) {
			sBuffer.append(" and loginname = '").append(loginname)
					.append("'");
		}
		String sql = sBuffer.toString();
		return (User) sqlMapClient.queryForObject("queryUser", sql);
	}

	@Override
	public int queryUserCount(User user) throws SQLException {
		String selectSql = " select count(id) from t_user where type <>0 and  ishide=0 ";
		StringBuffer andSql = new StringBuffer(32);
		andSql.append(selectSql);
		rtnWhereSql(user, andSql);
		return (Integer) sqlMapClient.queryForObject("queryUserCount",
				andSql.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> query(User user, int pageNow, int pageSize)
			throws SQLException {

		log.info(" query begin...");
		// TODO Auto-generated method stub
		StringBuffer andSql = new StringBuffer(32);

		String selectSql = "select * from t_user";
		String whereSql = " where type<>0 and ishide=0";
		andSql.append(selectSql).append(whereSql);
		rtnWhereSql(user, andSql);
		andSql.append(" order by updatetime desc limit ").append(pageNow)
				.append(",").append(pageSize);
		log.info(" query end...");
		return sqlMapClient.queryForList("queryUser", andSql.toString());
	}

	private void rtnWhereSql(User user, StringBuffer andSql) {
		if (user != null) {
			// 姓名
			if (!StringUtils.isBlank(user.getName())) {
				andSql.append(" and name like '%").append(user.getName())
						.append("%'");
			}
			// 用户类型
			if (null != user.getType() && -1 != user.getType()) {
				andSql.append(" and type =").append(user.getType());
			}
			// 用户id
			if (!StringUtils.isBlank(user.getUid())) {
				andSql.append(" and uid = '").append(user.getUid()).append("'");
			}
			// 用户名
			if (!StringUtils.isBlank(user.getLoginname())) {
				andSql.append(" and loginname like '%")
						.append(user.getLoginname()).append("%'");
			}
		}

	}

	@Override
	public int updateUser(User user) throws SQLException {
		// String updateSql = "update t_user set uid=uid";
		// StringBuilder sBuilder = buildModSql(user, updateSql);
		// sBuilder.append(" where uid ='").append(user.getUid()).append("'");
		Date now = new Date();
		user.setUpdatetime(now);
		return sqlMapClient.update("updateUser", user);
	}

	/**
	 * 构造修改sql
	 * 
	 * @param user
	 * @param updateSql
	 * @return sql语句
	 */
	public StringBuilder buildModSql(User user, String updateSql) {
		StringBuilder sBuilder = new StringBuilder(32);
		sBuilder.append(updateSql);
		if (!StringUtils.isBlank(user.getName())) {
			sBuilder.append(" , name ='").append(user.getName()).append("'");
		}
		if (!StringUtils.isBlank(user.getLoginname())) {
			sBuilder.append(",loginname='").append(user.getLoginname())
					.append("'");
		}
		if (!StringUtils.isBlank(user.getDescs())) {
			sBuilder.append(",descs='").append(user.getDescs()).append("'");
		}
		if (!StringUtils.isBlank(user.getPassword())) {
			sBuilder.append(",password='").append(user.getPassword())
					.append("'");
		}
		return sBuilder;
	}

	@Override
	public void hideUser(String uid) throws SQLException {
		sqlMapClient.update("hideUser", uid);

	}
}
